package com.jc.common.redis.trans;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import com.jc.common.redis.MyRedisException;

public class RedisTransactionStatus {
	private Transaction transaction;
	private Jedis redis;
	
	protected RedisTransactionStatus(Jedis redis,String... keys) throws MyRedisException {
		this.redis = redis;
		String ok = null;
		try {
			ok = this.redis.watch(keys);
		} catch (Exception e) {
			throw new MyRedisException("init redis transaction failed",e);
		}
		
		if (!"OK".equalsIgnoreCase(ok)){
			throw new MyRedisException("init redis transaction failed:" + ok);
		}
		
	}
	
	protected synchronized Jedis getRedis() {
		return redis;
	}
	
	protected synchronized void cleanRedis() {
		this.redis = null;
	}
	
	
	protected synchronized void setTransaction(Transaction transaction){
		if (transaction == null || this.transaction != null){
			return;
		}
		this.transaction = transaction;
	}
	
	public synchronized Transaction getTransaction() {
		return transaction;
	}
	
}
